package net.lgpage.tool;

import java.io.Serializable;

import android.app.Activity;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.app.Fragment;

public class Dispatcher {
	Activity activity;
	Fragment fragment;
	public Dispatcher(Activity activity) {
		super();
		this.activity = activity;
	}
	public Dispatcher(Fragment fragment){
		super();
		this.activity = fragment.getActivity();
		this.fragment = fragment;
	}
	
	public String intentGetObj(){
		return (String) intentGetObj(null);
	}
	public Object intentGetObj(Object defVal){
		return intentGetObj("obj", defVal);
	}
	public Object intentGetObj(String key,Object defVal){
		if(activity.getIntent() == null)return null;
		Intent intent = activity.getIntent();
		if(defVal==null||defVal instanceof String){
			String obj =  intent.getStringExtra(key);
			if(obj == null&&defVal!=null){
				return defVal;
			}else{
				return obj;
			}
		}else if(defVal instanceof Integer){
			return intent.getIntExtra(key, (int) defVal);
		}else if(defVal instanceof Long){
			return intent.getLongExtra(key, (long) defVal);
		}else if(defVal instanceof Float){
			return intent.getFloatExtra(key, (float) defVal);
		}else if(defVal instanceof Double){
			return intent.getDoubleExtra(key, (Double) defVal);
		}else if(defVal instanceof Boolean){
			return intent.getBooleanExtra(key, (boolean) defVal);
		}else if(defVal instanceof Parcelable){
			Parcelable obj =  intent.getParcelableExtra(key);
			if(obj == null&&defVal!=null){
				return defVal;
			}else{
				return obj;
			}
		}else{
			String json =  intent.getStringExtra(key);
			if(json == null)return null;
			return GsonTool.getGson().fromJson(json, (Class) defVal);
		}
	}
	
	
	public  void toActivity(Class clazz){
		toActivityForResult(clazz,1);
	}
	public  void toActivity(Class clazz,Object obj){
		toActivityForResult(clazz,obj,1);
	}
	public  void toActivityForResult(Class clazz,int requestCode){
		toActivityForResult(clazz, null, requestCode);
	}
	public  void toActivityForResult(Class clazz,Object obj,int requestCode){
		Intent it = new Intent();
		if(obj!=null){
			String key = "obj";
			if(obj instanceof Integer){
				it.putExtra(key, (Integer)obj);
			}else if(obj instanceof String){
				it.putExtra(key, (String)obj);
			}else if(obj instanceof Boolean){
				it.putExtra(key, (Boolean)obj);
			}else if(obj instanceof Float){
				it.putExtra(key, (Float)obj);
			}else if(obj instanceof Long){
				it.putExtra(key, (Long)obj);
			}else if(obj instanceof Serializable){
				it.putExtra(key, (Serializable)obj);
			}else if(obj instanceof Parcelable){
				it.putExtra(key, (Parcelable)obj);
			}else if(obj instanceof Double){
				it.putExtra(key, (Double)obj);
			}else{
				it.putExtra(key, GsonTool.getGson().toJson(obj));
			}
		}
		it.setClass(activity, clazz);
		if(fragment!=null){
			fragment.startActivityForResult(it, requestCode);
		}else{
			activity.startActivityForResult(it, requestCode);
		}
	}
}
